package com.bosssoft.ecds.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bosssoft.ecds.util.EntityUtil;
import com.bosssoft.ecds.dao.UbeStockFinanceapplyMapper;
import com.bosssoft.ecds.entity.dto.QueryDTO;
import com.bosssoft.ecds.entity.po.UbeStockFinanceapplyItemPO;
import com.bosssoft.ecds.entity.po.UbeStockFinanceapplyPO;
import com.bosssoft.ecds.entity.vo.UbeStockFinanceapplyItemVO;
import com.bosssoft.ecds.entity.vo.UbeStockFinanceapplyVO;
import com.bosssoft.ecds.service.AbstractService;
import com.bosssoft.ecds.service.UbeStockFinanceapplyService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 张东海
 * @date 2020/8/10
 * @description
 */
@Service
public class UbeStockFinanceapplyServiceImpl extends AbstractService<UbeStockFinanceapplyPO, UbeStockFinanceapplyMapper>
    implements UbeStockFinanceapplyService {

    @Resource
    RabbitTemplate rabbitTemplate;

    /**
     * 获取指定ID的单位票据申领信息所对应的申请具体票据信息
     * @param id
     * @return
     */
    @Override
    public List<UbeStockFinanceapplyItemVO> getItems(BigInteger id){
        List<UbeStockFinanceapplyItemPO> pos = mapper.getItems(id);
        return pos.stream()
                .map(po -> {
                    UbeStockFinanceapplyItemVO vo = new UbeStockFinanceapplyItemVO();
                    BeanUtil.copyProperties(po,vo);
                    return vo;
                })
                .collect(Collectors.toList());
    }

    /**
     * 查询指定查询条件下的单位票据申领信息记录
     * @param query
     * @return
     */
    @Override
    public List<UbeStockFinanceapplyVO> getAllDetail(QueryDTO query) {
        AbstractWrapper wrapper = new QueryWrapper<UbeStockFinanceapplyPO>();
        wrapper.eq("f_status",query.getChangeState());
        if(!StringUtils.isEmpty(query.getNo())){
            wrapper.eq("f_no",query.getNo());
        }
        if(!StringUtils.isEmpty(query.getAuthor())){
            wrapper.eq("f_author",query.getAuthor());
        }

        if(query.getPreDate() != null && query.getLastDate() != null){
            wrapper.between("f_submit_date",query.getPreDate(),query.getLastDate());
        }
        wrapper.last("LIMIT "+(query.getPage()-1)*query.getLimit()+","+query.getLimit());
        List<UbeStockFinanceapplyPO> pos = mapper.selectList(wrapper);
        return pos.stream()
                .map(po -> {
                    UbeStockFinanceapplyVO vo = new UbeStockFinanceapplyVO();
                    EntityUtil.POToVO(po, vo);
                    vo.setItems(mapper.getItems(po.getfId()));
                    return vo;
                })
                .collect(Collectors.toList());
    }

    /**
     * 将单位端的申领信息记录提交至财政端（中间件消息队列）
     * @param ubeStockFinanceapplyVO
     */
    public void submit(UbeStockFinanceapplyVO ubeStockFinanceapplyVO) {
        ubeStockFinanceapplyVO.setSubmitDate(LocalDateTime.now());
        List<UbeStockFinanceapplyItemVO> items = getItems(ubeStockFinanceapplyVO.getId());
        for ( UbeStockFinanceapplyItemVO item: items ) {
            rabbitTemplate.convertAndSend("amq.direct","itemKey",item);
        }
        rabbitTemplate.convertAndSend("amq.direct","theKey", ubeStockFinanceapplyVO);
    }

    /**
     * 重写AbstractService的insert方法，在新增申领信息前，自动生成单号
     * @param financeapplyPo
     * @return
     */
    @Override
    public int insert(UbeStockFinanceapplyPO financeapplyPo) {
        if(financeapplyPo.getfNo() == null){
            financeapplyPo.setFNo(new BigInteger("960000"));
        }
        else {
            financeapplyPo.setFNo(mapper.getMaxNo().add(new BigInteger("1")));
        }
        return super.insert(financeapplyPo);
    }

    /**
     * 获取当前申领信息表中的最大单号
     * @return
     */
    public BigInteger getMaxNo(){
        return mapper.getMaxNo();
    }

}
